<?php

declare(strict_types=1);
/*
 * @Description: GoEasy service类
 * @Author: QianLong
 * @Date: 2019-10-13 12:39:05
 * @LastEditors    : QianLong
 * @LastEditTime   : 2024-01-16 17:04:37
 */

namespace App\Service\Push;

use App\Common\RedisCache;
use Yurun\Util\HttpRequest;

class GoEasy
{
    protected $serverInfo = [];
    protected $http = null;
    protected $redisCache = null;
    public function __construct($serverInfo)
    {
        $this->serverInfo = $serverInfo;
        $this->http = new HttpRequest;
        $this->redisCache = new RedisCache();
    }

    /**
     * 直接推送
     */
    public function pushContent($param)
    {
        $param['content'] = is_array($param['content']) ? json_encode($param['content'], JSON_UNESCAPED_UNICODE) : $param['content'];
        $postFsQun['appkey'] = $this->serverInfo['platform_goeasy_common_key'];
        $postFsQun['channel'] = $this->serverInfo['platform_goeasy_channel'];
        $postFsQun['content'] = (isset($param['msg_content']) && !empty($param['msg_content'])) ? $param['msg_content'] : $param['content'];
        $response = $this->http->post($this->getUrl(), $postFsQun);
        $responseData = json_decode($response->body(), true);
        $return['send_result_data'] = $responseData;
        $return['pushParam'] = $postFsQun;
        $return['send_result'] = false;
        if (isset($responseData['code']) && $responseData['code'] == 200) {
            $return['send_result'] = true;
            $return['send_error'] = '';
        } else {
            $return['send_error'] = '(' . $responseData['code'] . ') ' . $responseData['content'];
        }
        unset($param, $postFsQun, $response, $responseData, $data);
        return $return;
    }
    /**
     * 直接推送
     */
    public function pushContentApp($param)
    {
        $param['content'] = is_array($param['content'])?json_encode($param['content'], JSON_UNESCAPED_UNICODE): $param['content'];
        $postFsQun['appkey'] = $this->serverInfo['platform_goeasy_common_key'];
        $postFsQun['channel'] = $this->serverInfo['platform_goeasy_channel'];
        $postFsQun['content'] = (isset($param['click_type_content']) && !empty($param['click_type_content'])) ? $param['click_type_content'] : $param['content'];
        $postFsQun['notification_title'] = $param['title'];
        $postFsQun['notification_body'] = $param['content'];
        $response = $this->http->post($this->getUrl(), $postFsQun);
        $responseData = json_decode($response->body(), true);
        $return['send_result_data'] = $responseData;
        $return['pushParam'] = $postFsQun;
        $return['send_result'] = false;
        if (isset($responseData['code']) && $responseData['code'] == 200) {
            $return['send_result'] = true;
            $return['send_error'] = '';
        } else {
            $return['send_error'] = '(' . $responseData['code'] . ') ' . $responseData['content'];
        }
        unset($param, $postFsQun, $response, $responseData, $data);
        return $return;
    }
    public function getUrl()
    {
        switch ($this->serverInfo['platform_goeasy_region']) {
            case 'hangzhou':
                return 'http://rest-hangzhou.goeasy.io/v2/pubsub/publish';
                break;
            case 'singapore':
                return 'http://rest-singapore.goeasy.io/v2/pubsub/publish';
                break;
            default:
                return 'http://rest-hangzhou.goeasy.io/v2/pubsub/publish';
                break;
        }
    }
}
